home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
src
/
mactech
/
volume14_1998
/
14.12.sit
/
14.12
/
OpenGL Part One.c
< prev
Wrap
C/C++ Source or Header
|
2006-09-22
|
4KB
|
167 lines
/* Rotating cube with color interpolation */
/* This program demonstrates the features of OpenGL */
/* discussed in the Mactech article: OpenGL for Mac Users: Part 1 */
/* For one button mouse use mouse, control-mouse, shift-mouse */
/* to choose between increment x, y or z angles */
/* q or Q key will terminate program */
/* 1 and 2 keys toggle between rotation and no rotation */
/* 3 and 4 keys toggle between opaque and translucent surfaces */
/* 5 and 6 keys toggle between smooth and falt shading */
/* 7 and 8 keys toggle hidden surface removal */
#include <stdlib.h>
#include "glut.h"
GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
{-1.0,1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {-1.0,1.0,1.0}, {1.0,1.0,1.0}};
GLfloat colors[][4] = {{0.0,0.0,0.0,0.5},{1.0,0.0,0.0,0.5},
{0.0,1.0,0.0,0.5}, {1.0,1.0,0.0,0.5}, {0.0,0.0,1.0,0.5},
{1.0,0.0,1.0,0.5}, {0.0,1.0,1.0,0.5}, {1.0,1.0,1.0,0.5}};
void quad(int a, int b, int c , int d)
{
/* draw a polygon via list of vertices */
/* assign a color to each vertex */
glBegin(GL_QUADS);
glColor4fv(colors[a]);
glVertex3fv(vertices[a]);
glColor4fv(colors[b]);
glVertex3fv(vertices[b]);
glColor4fv(colors[c]);
glVertex3fv(vertices[c]);
glColor4fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}
void cube(void)
{
/* map vertices to faces */
quad(3,1,0,2);
quad(2,6,7,3);
quad(6,2,0,4);
quad(1,3,7,5);
quad(4,5,7,6);
quad(5,4,0,1);
}
static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2;
void display(void)
{
/* display callback, clear frame buffer and z buffer,
rotate cube and draw, swap buffers */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
cube();
glutSwapBuffers();
}
/* Idle callback*/
void spinCube()
{
/* spin cube 2 degrees about selected axis */
theta[axis] += 2.0;
if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
glutPostRedisplay(); /* request redisplay */
}
/* mouse callback, selects an axis about which to rotate */
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; /* x axis */
if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; /* y axis */
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; /* z axis */
}
/* keyboard callback */
void mykey(unsigned char key, int i, int j)
{
if(key=='q'||key=='Q') exit(0); /* Terminate program */
if(key=='1') glutIdleFunc(NULL); /* Turn off rotation */
if(key=='2') glutIdleFunc(spinCube); /* Start rotation */
if(key=='3') /* Diable blending */
{
glDisable(GL_BLEND);
glutPostRedisplay();
}
if(key=='4') /* Enable blending */
{
glEnable(GL_BLEND);
glutPostRedisplay();
}
if(key=='5') /* Smooth shading */
{
glShadeModel(GL_SMOOTH);
glutPostRedisplay();
}
if(key=='6') /* Flat shading */
{
glShadeModel(GL_FLAT);
glutPostRedisplay();
}
if(key=='7') /* Hidden Surface Removal on */
{
glEnable(GL_DEPTH_TEST);
glutPostRedisplay();
}
if(key=='8') /* Hidden Surface Removal off */
{
glDisable(GL_DEPTH_TEST);
glutPostRedisplay();
}
}
/* Default user settings */
void myinit()
{
glClearColor(1.0, 1.0, 1.0, 1.0); /* Clear to white */
glMatrixMode(GL_PROJECTION); /* Set clipping window */
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0); /* 2 x 2 x 2 cube */
glMatrixMode(GL_MODELVIEW); /* Return to ModelView mode */
glShadeModel(GL_SMOOTH); /* Smooth Shading */
glEnable(GL_BLEND); /* Blending On */
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); /* Blend parameters */
}
void
main(int argc, char **argv)
{
glutInit(&argc, argv); /* Initialize GLUT */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
/* RGB color, double buffering and depth buffer */
glutInitWindowSize(200, 200); /* Request 200 x 200 window */
glutCreateWindow("Cube Demo"); /* Window title */
glutDisplayFunc(display); /* Display callback */
glutIdleFunc(NULL); /* Idle callback */
glutMouseFunc(mouse); /* Mouse callback */
glutKeyboardFunc(mykey); /* Keyboard callback */
myinit(); /* User settings */
glutMainLoop(); /* Enter event loop */
}